﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Setting.master" AutoEventWireup="true"
    CodeFile="Departments.aspx.cs" Inherits="Setting_Departments" %>

<asp:Content ID="Content1" ContentPlaceHolderID="SettingHeader" runat="Server">
    <link href="../Scripts/jquery-ui-1.8.14/smoothness/jquery-ui-1.8.14.custom.css" rel="stylesheet"
        type="text/css" />
    <script src="../Scripts/jquery-ui-1.8.14/jquery-ui-1.8.14.custom.min.js" type="text/javascript"></script>
    <link href="../Scripts/dynatree-1.1.1/skin-vista/ui.dynatree.css" rel="stylesheet"
        type="text/css" />
    <script src="../Scripts/dynatree-1.1.1/jquery.dynatree.js" type="text/javascript"></script>
    <style type="text/css">
        #treeContainer{border-bottom: dotted 1px #eeeeee;}
        #bToolbar{padding-top: 10px;}
        #dialog-addDept{display: none;}
    </style>
    <script type="text/javascript">
        var curDeptID = '0';
        var curDeptName = '所有部门';

        $(document).ready(function () {
            $("#tree").dynatree({
                initAjax: {
                    url: "/setting/do_LoadDept.aspx",
                    data: { id: "0" }
                },
                onActivate: function (node) {
                    curDeptID = node.data.key;
                    curDeptName = node.data.title;
                },
                onLazyRead: function (node) {
                    node.appendAjax({ url: "/setting/do_LoadDept.aspx",
                        data: { "id": node.data.key }
                    });
                }
            });

            $("#dialog-addDept").dialog({
                autoOpen: false,
                height: 300,
                width: 400,
                modal: true
            });
        });

        function AddDept() {
            if (curDeptID == "")
                window.location.reload();

            $("#DeptID").val("");
            $("#DeptName").val("");
            $("#ParentDeptID").val(curDeptID);
            $("#ParentDeptName").val(curDeptName);

            $("#btnSaveContinue").css("display", "");

            $("#dialog-addDept").dialog("open");
        }       

        function EditDept() {

            if (curDeptID == "")
                window.location.reload();

            if (curDeptID == '0') {
                alert('请选择要修改的部门!');
                return;
            }

            $("#DeptID").val(curDeptID);
            $("#DeptName").val(curDeptName);

            $.getJSON("do_GetDeptInfo.aspx", { id: curDeptID }, function (jsonData) {
                $("#ParentDeptID").val(jsonData[0].parentID);
                $("#ParentDeptName").val(jsonData[0].parentName);
            });

            $("#btnSaveContinue").css("display", "none");

            $("#dialog-addDept").dialog("open");
        }

        function DelDept() {
            if (curDeptID == "")
                window.location.reload();

            if (curDeptID == '0') {
                alert('请选择要删除的部门!');
            }
            else if (confirm('删除该部门，其下属部门也将被删除，部门中的人员将被移动到根部门下。\r\n\r\n您确认要删除 [' + curDeptName + '] 吗？')) {
                $.get("do_DelDept.aspx", { delid: curDeptID },
					  function (data) {
					      if (data == "ok") {
					          $("#tree").dynatree("getTree").getNodeByKey(curDeptID).remove();
					      }
					      else
					          alert("删除失败!");
					  });
            }

        }

        function doSaveDept() {
            //validate
            if ($("#DeptName").val() == "") {
                alert("部门名称不能为空!");
                return;
            }
            //submit
            $.post("do_SaveDept.aspx", { deptID: $("#DeptID").val(), deptName: $("#DeptName").val(), parentID: $("#ParentDeptID").val() },
			        function (data) {
			            if (data == "ok") {
			                if ($("#btnSaveContinue").css("display") == "none") {
			                    var node = $("#tree").dynatree("getTree").getNodeByKey(curDeptID);
			                    node.data.title = $("#DeptName").val();
			                    node.render();

			                    curDeptName = $("#DeptName").val();
			                    $("#dialog-addDept").dialog("close");
			                }
			                else {
			                    window.location.reload();
			                }
			            }
			            else
			                alert("保存失败!");
			        });
        }

        function doSaveDeptContinue() {
        	//validate
        	if ($("#DeptName").val() == "") {
        		alert("部门名称不能为空!");
        		return;
        	}
        	//submit
        	$.post("do_SaveDept.aspx", { deptID: $("#DeptID").val(), deptName: $("#DeptName").val(), parentID: $("#ParentDeptID").val() },
		            function (data) {
		            	if (data == "ok") {
		            		var node = $("#tree").dynatree("getTree").getNodeByKey(curDeptID);
		            		var childNode = node.addChild({
		            			title: $("#DeptName").val(),
		            			key: $("#DeptID").val()
		            		});
		            		$("#DeptName").val("");
		            	}
		            	else
		            		alert("保存失败!");
		            });
        }

        function SortDept() {
        	window.location = "deptsort.aspx?pid=" + curDeptID;
		}
	</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="SettingContent" runat="Server">
    <div class="navTitle">
        <a href="#">系统设置</a> > 部门</div>
    <div id="treeContainer">
        <div id="tree">
        </div>
    </div>
    <div id="bToolbar">
        <input type="button" value="添加" onclick="AddDept()" />&nbsp;<input type="button"
            value="修改" onclick="EditDept()" />&nbsp;<input type="button" value="删除" onclick="DelDept()" />&nbsp;<input type="button" value="排序" onclick="SortDept()" /></div>
    <div id="dialog-addDept" title="添加部门">
        <br /><br />
        <table cellpadding="3" align="center" >
            <tr>
                <td>
                    部门名称
                </td>
                <td>
                    <input type="text" id="DeptName" name="DeptName" /><input type="hidden" id="DeptID"
                        name="DeptID" />
                </td>
            </tr>
            <tr>
                <td>
                    上级部门
                </td>
                <td>
                    <input type="text" id="ParentDeptName" name="ParentDeptName" readonly /><input type="hidden"
                        id="ParentDeptID" name="ParentDeptID" />
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <input type="button" id="btnSave" value="保存" onclick="doSaveDept()" />&nbsp;<input
                        type="button" id="btnSaveContinue" value="保存并继续" onclick="doSaveDeptContinue()" />
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <span id="tipSpan"></span>
                </td>
            </tr>
        </table>
    </div>
</asp:Content>
